#include "rexplorer.h"
#include "ui_rexplorer.h"

rexplorer::rexplorer(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::rexplorerClass)
{
    ui->setupUi(this);

    setActiveListView(ui->listFile1);

    fsMdl = new QFileSystemModel;

    ui->treeDir->setModel(fsMdl);
    ui->treeDir->setRootIndex(fsMdl->setRootPath(fsMdl->myComputer().toString()));

    //hide detail columns
    ui->treeDir->setColumnHidden(1,true);
    ui->treeDir->setColumnHidden(2,true);
    ui->treeDir->setColumnHidden(3,true);

    /** load settings **/
    {   settings = new QSettings("Roger Grayson","rexplorer");
        this->resize(settings->value("Size",QSize(660,400)).toSize());
        this->move(settings->value("Position").toPoint());
        this->viewDirTree(settings->value("DirTree",true).toBool());
        this->viewDualPane(settings->value("DualPane",true).toBool());

        //restore listFile locations
        ui->listFile1->setModel(fsMdl);
        ui->listFile1->reindexFileView(fsMdl->index(settings->value("Pane1Loc",QDir::homePath()).toString()));

        ui->listFile2->setModel(fsMdl);
        ui->listFile2->reindexFileView(fsMdl->index(settings->value("Pane2Loc",QDir::homePath()).toString()));
    }
    connect(ui->actionE_xit, SIGNAL(triggered()), this, SLOT(close()));

    connect(ui->listFile1, SIGNAL(setActive(QListView*)), this, SLOT(setActiveListView(QListView*)));
    connect(ui->listFile2, SIGNAL(setActive(QListView*)), this, SLOT(setActiveListView(QListView*)));
}

rexplorer::~rexplorer()
{
    settings->setValue("Size",this->size());
    settings->setValue("Position",this->pos());
    settings->setValue("Pane1Loc",fsMdl->filePath(ui->listFile1->rootIndex()));
    settings->setValue("Pane2Loc",fsMdl->filePath(ui->listFile2->rootIndex()));
    settings->setValue("DirTree",ui->actionDirectory_Tree->isChecked());
    settings->setValue("DualPane",ui->actionDual_File_Panes->isChecked());

    delete ui;
    delete fsMdl;
    delete settings;
}

void rexplorer::setActiveListView(QListView *active)
{
    QColor qcinactive(232,232,232);
    QColor qcactive(255,255,255);
    QPalette lf1p(ui->listFile1->palette());
    QPalette lf2p(ui->listFile2->palette());

    //TODO:optimize
    if(active == ui->listFile1)
    {
        ui->listFile1->setEnabled(true);
        activeListView = ui->listFile1;
        ui->actionSet_View_1_Active->setChecked(true);
        ui->actionSet_View_2_Active->setChecked(false);
        lf1p.setColor(QPalette::Base,qcactive);
        lf2p.setColor(QPalette::Base,qcinactive);
    }
    else
    {
        ui->listFile2->setEnabled(true);
        activeListView = ui->listFile2;
        ui->actionSet_View_1_Active->setChecked(false);
        ui->actionSet_View_2_Active->setChecked(true);
        lf1p.setColor(QPalette::Base,qcinactive);
        lf2p.setColor(QPalette::Base,qcactive);
    }
    ui->listFile1->setPalette(lf1p);
    ui->listFile2->setPalette(lf2p);
}

void rexplorer::viewDirTree(bool view)
{
    ui->actionDirectory_Tree->setChecked(view);
    ui->treeDir->setVisible(view);
}

void rexplorer::viewDualPane(bool view)
{
    ui->actionDual_File_Panes->setChecked(view);
    ui->listFile2->setVisible(view);
    ui->lblPane2->setVisible(view);
}

void rexplorer::on_treeDir_activated(QModelIndex index)
{
    activeListView->reindexFileView(index);
}

void rexplorer::on_actionSet_View_1_Active_activated()
{
    setActiveListView(ui->listFile1);
}

void rexplorer::on_actionSet_View_2_Active_activated()
{
    setActiveListView(ui->listFile2);
}

void rexplorer::on_actionDrives_triggered()
{
    QMenu *menu = new QMenu(this);
    QAction *action = NULL;

    QFileInfoList drives = QDir::drives();
    for(int i=0; i<drives.size(); i++)
    {
        action = new QAction(drives.at(i).filePath(),this);
        action->setData(drives.at(i).filePath());
        //action->setIcon();
        menu->addAction(action);
    }

    if( (action = menu->exec(QCursor::pos(), action)) )
    {
        activeListView->reindexFileView(fsMdl->index(action->data().toString()));
    }
}

void rexplorer::on_actionSystem_triggered()
{
    QMenu *menu = new QMenu(this);
    QAction *action = NULL;

    action = new QAction("Root",this);
    action->setData(QDir::rootPath());
    menu->addAction(action);

    action = new QAction("Home",this);
    action->setData(QDir::homePath());
    menu->addAction(action);

    #ifdef Q_OS_WIN
        QSettings regSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", QSettings::NativeFormat);
        menu->addSeparator();

        action = new QAction("Documents",this);
        action->setData(regSettings.value("Personal").toString());
        menu->addAction(action);

        action = new QAction("Recent Items",this);
        action->setData(regSettings.value("Recent").toString());
        menu->addAction(action);

        action = new QAction("Desktop",this);
        action->setData(regSettings.value("Desktop").toString());
        menu->addAction(action);
    #endif

    if( (action = menu->exec(QCursor::pos(), action)) )
    {
        activeListView->reindexFileView(fsMdl->index(action->data().toString()));
    }
}

void rexplorer::on_actionBack_triggered()
{   //TODO
}

void rexplorer::on_actionDirectory_Tree_triggered()
{
    viewDirTree(!ui->treeDir->isVisible());
}

void rexplorer::on_actionDual_File_Panes_triggered()
{
    viewDualPane(!ui->listFile2->isVisible());
}

void rexplorer::on_lblPane1_linkActivated(QString link)
{
    reindexFileViewByLink(ui->lblPane1,link);
}

void rexplorer::on_lblPane2_linkActivated(QString link)
{
    reindexFileViewByLink(ui->lblPane2,link);
}

void rexplorer::reindexFileViewByLink(QLabel *qlp, QString link)
{
    QWidget *qwp = qlp->parentWidget();
    if(qwp)
    {
        QObjectList qol = qwp->children();
        for(int i=0;i<qol.size(); i++)
        {   QObject* qo = qol.at(i);
            //QMessageBox::information(this,"name",qo->objectName(),QMessageBox::Ok);
            if( qo->objectName().contains("listFile") )
            {   ((QCustomListView*)qo)->reindexFileView(fsMdl->index(link));
            }
        }
    }
}
